<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-plus" @click="() => addMenu()">添加</el-button>
    </div>
    <hr >
    <el-row :gutter="24">
      <el-col :span="9">
        <div class="grid-content bg-purple">
          <div class="custom-tree-container">
            <div class="block">
              <el-tree :props="props" :data="menus" :expand-on-click-node="false" node-key="id" default-expand-all>
                <span slot-scope="{ node, data }" class="custom-tree-node">
                  <span>{{ node.label }}</span>
                  <span v-if="data.id!='0'">
                    <el-button type="text" size="mini" @click="() => updateMenu(node,data)">
                      编辑
                    </el-button>
                    <el-button type="text" size="mini" @click="() => removeMenu(node, data)">
                      删除
                    </el-button>
                  </span>
                </span>
              </el-tree>
            </div>
          </div>
        </div>
      </el-col>

    </el-row>

    <el-dialog :visible.sync="dialogFormVisible" title="菜单操作" center width="30%" @close="dialogClose()">
      <el-form :model="form">
        <el-form-item :label-width="formLabelWidth" label="菜单名称">
          <el-input v-model="form.title" autocomplete="off"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="父级菜单">
          <el-select v-model="form.parentId" placeholder="请选择父级菜单">
            <el-option :value="0" label="主菜单">主菜单</el-option>
            <el-option v-for="item in menus" :label="item.title" :value="item.id">{{ item.title }}</el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="菜单路由">
          <el-input v-model="form.url" autocomplete="off"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="菜单图标">
          <el-input v-model="form.icon" autocomplete="off"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="是否可用">
          <el-switch v-model="form.flag==='1'" active-color="#13ce66" inactive-color="#ff4949" @change="flagOpen()"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="菜单描述">
          <el-input v-model="form.description" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getAllMenus,
  deleteMenu,
  getMenuById,
  saveOrUpdateMenu
} from '@/api/sys/menu'
export default {
  name: 'Menu',
  components: {},
  data() {
    return {
      props: {
        children: 'menus',
        label: 'title'
      },
      menus: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        id: '',
        title: '',
        icon: '',
        parentId: '',
        description: '',
        url: '',
        flag: '1'
      },
      formLabelWidth: '90px'
    }
  },
  mounted: function() {
    this.findMenusAll()
  },

  methods: {
    removeMenu(node, data) {
      this.$confirm('该操作敏感, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteMenu(data.id).then(response => {
          this.findMenusAll()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    findMenusAll() {
      getAllMenus().then(response => {
        const data = response.result
        this.menus = data
      })
    },
    updateMenu(node, data) {
      this.dialogFormVisible = true
      getMenuById(data.id).then(response => {
        this.form = response.result
      })
    },
    addMenu() {
      this.dialogFormVisible = true
    },
    flagOpen() {
      const flag = this.form.flag
      if (flag === '0') {
        this.form.flag = '1'
      } else {
        this.form.flag = '0'
      }
    },
    dialogClose() {
      this.form.id = ''
      this.form.title = ''
      this.form.icon = ''
      this.form.parentId = ''
      this.form.description = ''
      this.form.url = ''
      this.form.flag = '0'
    },
    submit() {
      saveOrUpdateMenu(this.form).then(response => {
        this.findMenusAll()
        this.$message.success('菜单保存成功')
        this.dialogFormVisible = false
      })
    }

  }
}
</script>
<style>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>
